%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DOCUMENT TITLE : OMR-Binarization Function
%
% EXPLANATION    : This function takes file name of a gray scale image,
%                  binarizes the image, writes the binary image to a
%                  file and outputs the binary image.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function binary_image = binarize(gray_scale_image_fn)

I = imread(gray_scale_image_fn); % read the gray scale image
threshold_level = graythresh(I); % find threshold level
binary_image = im2bw(I,threshold_level); % binarize the image
binary_image_fn = ['bin-' gray_scale_image_fn]; % add prefix "bin-" to input file name for binary image file name
imwrite(binary_image,binary_image_fn,'png','bitdepth',1); % write the binary image to a file